package com.example.animalcase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private EditText mLoginEmail;
    private EditText mLoginPassword;
    private Button mLoginButton;
    private TextView mSignupRedirectText;

    DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        mLoginEmail=(EditText)findViewById(R.id.login_email);
        mLoginPassword=(EditText)findViewById(R.id.login_password);
        mLoginButton=(Button) findViewById(R.id.login_button);
        mSignupRedirectText=(TextView) findViewById(R.id.signupRedirectText);

        databaseHelper=new DatabaseHelper(this);

        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email=mLoginEmail.getText().toString();
                String password=mLoginPassword.getText().toString();

                if (email.equals("")||password.equals("")){
                    Toast.makeText(LoginActivity.this,"All fields are mandatory",Toast.LENGTH_SHORT).show();
                }else {
                    Boolean checkCredntials=databaseHelper.checkEmailPassword(email,password);

                    if (checkCredntials == true){
                        Toast.makeText(LoginActivity.this,"Login Succseefully",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
                        startActivity(intent);
                    }else {
                        Toast.makeText(LoginActivity.this,"Invalid Credentials",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        mSignupRedirectText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),SignupActivity.class);
                startActivity(intent);
            }
        });
    }
}
